package com.mdxx.xczd.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;
import com.mdxx.xczd.R;
import com.mdxx.xczd.aidl.ProcessService;

public class GuardService extends Service {

	  private MyBinder binder;
	    private MyConn conn;
	 
	    @Override
	    public IBinder onBind(Intent intent) {
	        return binder;
	    }

	    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	    @Override
	    public void onCreate() {
	        super.onCreate();
	        binder = new MyBinder();
	        if(conn==null)
	            conn = new MyConn();
	    }

	    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	    @Override
	    public void onStart(Intent intent, int startId) {
	        super.onStart(intent, startId);
	        Notification.Builder builder = new Notification.Builder(this);
	        builder.setSmallIcon(R.drawable.ic_launcher);
	        startForeground(250, builder.build());
	        startService(new Intent(this,NotificationService.class));
	        GuardService.this.bindService(new Intent(this,LocationService.class),conn, Context.BIND_IMPORTANT);
	    }

	    class MyBinder extends ProcessService.Stub{

	        @Override
	        public String getServiceName() throws RemoteException {
	            return "I am SecondService";
	        }
	    }

	    class  MyConn implements ServiceConnection {

	        @Override
	        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
	            Log.i("Info","与FirtService连接成功"+GuardService.this.getApplicationInfo().processName);
	        }

	        @Override
	        public void onServiceDisconnected(ComponentName componentName) {
	            /*Toast.makeText(GuardService.this,"FirstService被杀死",Toast.LENGTH_SHORT).show();*/
	            GuardService.this.startService(new Intent(GuardService.this,LocationService.class));
	            GuardService.this.bindService(new Intent(GuardService.this,LocationService.class),conn, Context.BIND_IMPORTANT);
	        }
	    }
}